var ns, toast, actionsheet, WXPhotoPicker;
var app = new Vue({
    el: '#app',
    data: {
        TaskID:0,
        ID:0,
        form: {
            ID: 0,
            TaskContent:'',   
        },
        DelImgIDs:[],
        task_imgList:[],
        imglist: [],
        maxuploadcount: CONFIG.maxuploadcount,
    },
    methods: {
        get_data(){
            var that = this;
            var options = {};
            options.ID = that.ID;    
            options.action = "getworktaskrecorddetail";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.form.TaskContent=data.data.ContentDesc;
                    that.task_imgList=data.data.FileList;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        do_save: function() {
            var that = this;
            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                filelist.push(that.imglist[i].url);
            }
            var options = {};
            options.ID = that.ID;    
            options.WorkTaskID = that.TaskID;  
            options.action = "saveworktaskrecord";
            options.ContentDesc = that.form.TaskContent;
            options.DeleteFileIDs=JSON.stringify(that.DelImgIDs);
            // console.log(JSON.stringify(options));
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    ns.toast('保存成功');
                    api.sendEvent({
                        name: 'updata_task_detail',
                        extra: {
                            key1: 'value1'
                        }
                    });
                    setTimeout(function() {
                        api.closeWin();
                    }, 1000);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                files: {
                    file: filelist
                },
                toast: true,
                toastmsg: '提交中'
            });
        },
        choose_picture: function() {
            var that = this;
            ns.confirmPer('camera', function() {
                that.choose_picture_start();
            })
        },
        choose_picture_start:function(){
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.actionSheet({
                cancelTitle: '取消',
                buttons: ['拍照', '从相册选择']
            }, function(ret, err) {
                //拍照
                if(ret.buttonIndex==1){ 
                    that.upload_choose_picture();
                    return;
                }
                //从相册选择
                if(ret.buttonIndex==2){
                    that.choose_picture_album();
                    return;
                } 
                //取消 
                else{
                    return false;  
                }  
            });
        },
        upload_choose_picture:function(){
            var that = this;
            api.getPicture({
                sourceType: 'camera',
                encodingType: 'jpg',
                destinationType: 'url',
                allowEdit: false,
                saveToPhotoAlbum: true,
            }, function(ret, err) {
                if (ret&&ret.data) {
                    that.imglist.push({
                        ID: 0,
                        url: ret.data,
                        thumbPath: ret.data,
                        isupload: true
                    });
                } 
            });
        },
        choose_picture_album:function(){
            var that = this;
            WXPhotoPicker.open({
                max: that.maxuploadcount - that.imglist.length,
                type:'image',
            }, function(ret) {
                if (ret) {
                    if (ret.list.length > 0) {
                        ret.list.forEach((item) => {
                            that.imglist.push({
                                ID: 0,
                                url: item.path,
                                thumbPath: item.path,
                                isupload: true
                            });
                        });
                    }
                }
            });
        },
        remove_img: function (index,item) {
            var that = this;
            that.imglist.splice(index, 1);
        },
        remove_img_edit: function (ID) {
            var that = this;
            for (var i = 0; i < that.task_imgList.length; i++) {
                var item = that.task_imgList[i];
                if (item.ID == ID) {
                    that.DelImgIDs.push(ID);//保存需要删除的图片ID
                    that.task_imgList.splice(i, 1);
                }
            }
        },
        do_viewimages: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].url);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
        do_viewimages_edit: function (activeIndex) {
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.task_imgList.length; i++) {
                imageUrls.push(that.task_imgList[i].SysFinalFilePath);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
    }
});

apiready = function() {
    ns = window.Foresight.Util;
    toast = new auiToast();
    actionsheet = new auiActionsheet();
    WXPhotoPicker = api.require('WXPhotoPicker');
    app.ID = api.pageParam.id || 0;
    app.TaskID = api.pageParam.taskid || 0;
    if(app.ID){
        app.get_data();
    }
}
